package main

import (
	"caselib/controllers"
	_ "caselib/routers"
	"net/url"

	"github.com/astaxie/beego/context"
	"github.com/beego/beego/v2/server/web"

	"github.com/astaxie/beego"
)

var FilterUser = func(ctx *context.Context) {
	username, _ := url.QueryUnescape(ctx.GetCookie("username"))
	if ctx.Request.RequestURI != "/login" && ctx.Request.RequestURI != "/register.html" && ctx.Request.RequestURI != "/signup" && ctx.Request.RequestURI != "/back.jpg" && !(len(ctx.Request.RequestURI) > 3 && ctx.Request.RequestURI[len(ctx.Request.RequestURI)-3:] == ".js") && username == "" {
		ctx.Redirect(302, "/login")
	}
}

func main() {
	controllers.UserToTime = make(map[string]controllers.IndexAndTime)
	beego.BConfig.WebConfig.TemplateLeft = "<<<"
	beego.BConfig.WebConfig.TemplateLeft = ">>>"
	web.BConfig.RecoverPanic = true
	beego.InsertFilter("/*", beego.BeforeRouter, FilterUser)
	beego.SetStaticPath("/static", "static")
	beego.Run()
}
